package com.maikang.exammall.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.maikang.exammall.converter.JpaConverterIntegerSetBinaryInteger;
import com.maikang.exammall.converter.JpaConverterStringListJson;
import com.maikang.exammall.entity.constant.RepositoryConstant;
import com.maikang.exammall.entity.dto.PriceIntervalDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.hibernate.validator.constraints.Range;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

import static com.maikang.exammall.entity.Hospital.TABLE_NAME;

@Entity
@Table(name = TABLE_NAME, schema = RepositoryConstant.APP_DB_TABLE_SCHEMA, catalog = RepositoryConstant.APP_DB_TABLE_SCHEMA)
@DynamicUpdate
@DynamicInsert
@SuperBuilder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler"})
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString(callSuper=true, exclude = {"packages"})
@EqualsAndHashCode(callSuper=true, exclude = {"packages"})
@SQLDelete(sql = "update " + RepositoryConstant.APP_DB_TABLE_SCHEMA + "." + TABLE_NAME + " set is_deleted = 1 WHERE id = ?")
@Where(clause = " is_deleted = 0")
public class Hospital extends AuditEntity<Long> {
    public static final String TABLE_NAME = "mkh_hospital";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String address;
    private String avatarUrl;
    @Convert(converter = JpaConverterStringListJson.class)
    private List<String> imgUrls;
    private String intro;
    @ApiModelProperty("0.无级别;1-公立三甲 2-公立医院 3-民营医院 4-专业机构")
    @Range(max = 4, min = 0)
    private Integer level;
    @ApiModelProperty("1-7表示周一到周日")
    @Convert(converter = JpaConverterIntegerSetBinaryInteger.class)
    private Set<Integer> openDates;
    private Long provinceId;
    private Long cityId;
    private Long countyId;
    private Long townId;
    private Double longitude;
    private Double latitude;
    @ApiModelProperty("点击立即下单数")
    private Integer orderCount;

    @ApiModelProperty(hidden = true)
    @Transient
    private Double distance;

    @ApiModelProperty(hidden = true)
    @Transient
    private PriceIntervalDTO priceIntervalDTO;

    @ApiModelProperty(hidden = true)
    @Transient
    private Long lowestRealPrice;

    @OneToMany(mappedBy = "hospital", fetch = FetchType.LAZY, cascade = CascadeType.ALL)   // CascadeType: MERGE: 更新， PERSIST: 保存, REMOVE: 删除
    @JsonBackReference
    private List<Package> packages;

    @Override
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public Long getPrimaryKey() {
        return this.id;
    }

    @Override
    public void setPrimaryKey(Long id) {
        this.id = id;
    }
}
